import React, { useState } from 'react';
import {
  DesktopOutlined,
  FileOutlined,
  PieChartOutlined,
  TeamOutlined,
  UserOutlined,
} from '@ant-design/icons';
import { Breadcrumb, Layout, Menu, theme } from 'antd';
import { secondRoutes2 } from '../../router';
import { NavLink, Outlet, useLocation } from 'react-router-dom';
const { Header, Content, Footer, Sider } = Layout;
function getItem(label, key, icon, children) {
  return {
    key,
    icon,
    children,
    label,
  };
}
 //二级路由渲染
  const renderItems = (arr)=>{
    let items=[]
    arr && arr.map(item=>{
        if(item.children)
        {
           items.push(getItem(<NavLink to={item.path}>{item.title}</NavLink>, item.path, <PieChartOutlined />,renderItems(item.children)))
        }else{ 
             items.push(getItem(<NavLink to={item.path}>{item.title}</NavLink>, item.path, <PieChartOutlined />))
        }
       
    })
    return items
  }

  const items = renderItems(secondRoutes2)

function Index() {

    //获取当前路径
  const location = useLocation()
  const paths = location.pathname.split('/').slice(1)
   
   const [collapsed, setCollapsed] = useState(false);
  const {
    token: { colorBgContainer, borderRadiusLG },
  } = theme.useToken();
  return (
    <div>
       <Layout
      style={{
        minHeight: '100vh',
      }}
    >
      <Sider collapsible collapsed={collapsed} onCollapse={(value) => setCollapsed(value)}>
        <div className="demo-logo-vertical" />
        <Menu theme="dark" defaultSelectedKeys={['1']} mode="inline" items={items} />
      </Sider>
      <Layout>
        <Header
          style={{
            padding: 0,
            background: colorBgContainer,
          }}
        />
        <Content
          style={{
            margin: '0 16px',
          }}
        >
          <Breadcrumb
            style={{
              margin: '16px 0',
            }}
          >
             {/* 面包屑 */}
          <Breadcrumb.Item>
              <NavLink to='/home'>返回主页</NavLink>
          </Breadcrumb.Item>
          {
            paths && paths.map((v,i)=>{
                return  <Breadcrumb.Item key={i}>
                    <NavLink to={`/${paths.slice(0,i+1).join('/')}`}>{v}</NavLink>
                </Breadcrumb.Item>
            })
          }
          </Breadcrumb>
          <div
            style={{
              padding: 24,
              minHeight: 360,
              background: colorBgContainer,
              borderRadius: borderRadiusLG,
            }}
          >
             {/* 二级路由出口 */}
           <Outlet/>
          </div>
        </Content>
        <Footer
          style={{
            textAlign: 'center',
          }}
        >
          Ant Design ©{new Date().getFullYear()} Created by Ant UED
        </Footer>
      </Layout>
    </Layout>
    </div>
  )
}

export default Index
